package cate.game.attr;

import cate.common.table.d.GDPosition;
import cate.game.framework.ModuleMan;
import cate.game.play.part.PlaySide;
import cate.game.play.support.PlayBuildContext;
import cate.game.role.Role;
import cate.game.role.bag.hero.Hero;
import easy.java.dev.note.NoteClass;
import easy.java.practice.errorcode.EcResult;

import java.util.function.Predicate;

@NoteClass("战力管理")
public class PowerManager extends ModuleMan {

	@Override
	public void initDependent() throws Exception {

	}


	/**
	 * 计算阵容战力
	 * @param role
	 * @param funcId
	 * @return
	 */
	public long calcPositionPower(Role role, int funcId) {
		return calPositionPower(role, funcId, GDPosition.DEFAULT_POS_NUM);
	}


	/**
	 * 计算阵容战力
	 * @param role
	 * @param funcId
	 * @param posNum
	 * @return
	 */
	public long calPositionPower(Role role, int funcId, int posNum) {
		EcResult<PlaySide> r = game.fight.side.createPlaySideByRole(role, funcId, posNum);
		if (!r.ok()) {
			return 0;
		}
		return r.data.getTotalPower();
	}

	/**
	 * 计算怪物实例战力

	 * @return
	 */
	public long calByDynCode(String code) {
		return calByDynCode(code,0);
	}

	/**
	 * 计算动态怪物战力
	 * @param code
	 * @param heroLevel
	 * @return
	 */
	public long calByDynCode(String code, int heroLevel){
		EcResult<PlaySide> r = game.fight.side.createPlaySideByCode(code, heroLevel);
		if (!r.ok()) {
			return 0;
		}
		return r.data.getTotalPower();
	}

	/**
	 * 根据属性计算战力
	 *
	 * 会把二阶属性转化为一阶属性再计算战力
	 *
	 * @param attr
	 * @return
	 */
	public static long calcByAttr(FightAttr attr, PlayBuildContext ctx) {
		if (attr == null || attr.raw == null) {
			return 0;
		}
		double power = 0;
		for (Integer key : attr.raw.keySet()) {
			double value = attr.getTotalValue(key);
			power += ctx.attrExcel().base.getRower(key) * value;
		}
		return (long) power;
	}

	/**
	 * 标记战力过期
	 * @param role
	 * @param action
	 */
	public void markPowerExpire(Role role, Predicate<Hero> action) {
		for (Hero hero : role.getBag().hero.getList()) {
			if (action.test(hero)) {
				hero.markPowerExpire();
			}
		}
	}

}
